package com.jz.legalpersonclass.conroller;

import com.jz.legalpersonclass.model.*;
import com.jz.legalpersonclass.service.LegalPersonService;
import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by WISE on 2021/12/26.
 */
@RestController
@CrossOrigin//开启跨域
@RequestMapping("legalPerson")
public class LegalPersonController {

    ///1.下拉框
    @PostMapping("getPullDownList")
    public ResultVo userDown() {

        Integer id = 1;
        return  legalPersonService.queryDown(id);


    }

    //1.首页分页
    @Autowired
    private LegalPersonService legalPersonService;

    @PostMapping("queryPage")
    public ResultVo firstPage(@RequestBody LegalPersonSelectVo legalPersonSelectVo) {
        ResultVo resultVo = legalPersonService.firstPage(legalPersonSelectVo);

        return resultVo;
    }

    //2.新增
    @PostMapping("add")
    @Transactional
    public ResultVo add(@RequestBody @Valid LegalPersonAddVo legalPersonAddVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = legalPersonService.addSubClass(legalPersonAddVO);
        return resultVo;
    }


    //3.修改
    @GetMapping("queryById")
    @Transactional
    public ResultVo queryById(Integer id) {

        if (id == null) {
            return ResultBuildVo.error("ID不能为空");
        }
        return legalPersonService.queryById(id);
    }


    @PostMapping("update")
    public ResultVo update(@RequestBody @Valid LegalPersonUpdateVo legalPersonUpdateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.updateSubClass(legalPersonUpdateVo);
    }

    @PostMapping("updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid LegalPersonUpdateStatusVo legalPersonUpdateStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.updateStatus(legalPersonUpdateStatusVo);
    }


}



